package com.bawei.module_base.ui.view

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.core.view.setMargins
import com.bawei.module_base.R

class StripTabLayout @JvmOverloads constructor(
    context:Context,attributeSet: AttributeSet? = null,
):LinearLayout(context,attributeSet){

    //初始化
    init {
        orientation= HORIZONTAL
        addView(getTabBg())
        addView(getTabBg())
        addView(getTabBg())
        layoutParams.apply {
            gravity=Gravity.CENTER_VERTICAL
        }
        setStep(0)
        setPageChangeListener { null }
    }
    //点击切换界面
    fun setPageChangeListener(listener: ((Int) -> Unit)?) {
        getChildAt(0).setOnClickListener { listener?.invoke(0) }
        getChildAt(1).setOnClickListener { listener?.invoke(1) }
        getChildAt(2).setOnClickListener { listener?.invoke(2) }

    }
    //赋值给选中切换哪个
    fun setStep(i: Int) {
        getChildAt(0).isSelected = i >= 0
        getChildAt(1).isSelected = i >= 1
        getChildAt(2).isSelected = i >= 2
    }
    //布局
    private fun getTabBg(): View {
        return LayoutInflater.from(context).inflate(R.layout.strip_item, null, false)
            .also {
//                个性化的布局参数。
                val par = LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT)

                par.height = 40
                par.weight = 1.0F
                par.setMargins(10)
                it.layoutParams = par
            }

    }
}